<template>
<view class="content">
	<view :style='{"minHeight":"100%","width":"100%","padding":"0","position":"relative","background":"#fff","height":"auto"}'>
		<form :style='{"width":"100%","padding":"24rpx","background":"none","display":"block","height":"auto"}' class="app-update-pv">
			<view :style='{"padding":"12rpx 0","margin":"0 0 24rpx 0","borderColor":"rgb(119, 47, 26)","alignItems":"center","borderWidth":"0","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"padding":"0 10rpx 0 0","whiteSpace":"nowrap","color":"#000000","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">服务标题</view>
				<input :style='{"padding":"0px 24rpx","margin":"0px","borderColor":"#808080","color":"rgb(0, 0, 0)","flex":"1","borderWidth":"0 0 2rpx 0","lineHeight":"80rpx","fontSize":"28rpx","borderStyle":"solid"}' :disabled="ro.fuwubiaoti" v-model="ruleForm.fuwubiaoti" placeholder="服务标题"  type="text"></input>
			</view>
			<view :style='{"padding":"12rpx 0","margin":"0 0 24rpx 0","borderColor":"rgb(119, 47, 26)","alignItems":"center","borderWidth":"0","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class=" select">
				<view :style='{"padding":"0 10rpx 0 0","whiteSpace":"nowrap","color":"#000000","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">服务类型</view>
				<picker :disabled="ro.fuwuleixing" :style='{"width":"100%","flex":"1","height":"auto"}' @change="fuwuleixingChange" :value="fuwuleixingIndex" :range="fuwuleixingOptions">
					<view :style='{"borderColor":"#707070","color":"#707070","borderWidth":"0 0 2rpx 0","width":"100%","lineHeight":"80rpx","fontSize":"28rpx","borderStyle":"solid"}' class="uni-input">{{ruleForm.fuwuleixing?ruleForm.fuwuleixing:"请选择服务类型"}}</view>
				</picker>
			</view>
			<view :style='{"padding":"12rpx 0","margin":"0 0 24rpx 0","borderColor":"rgb(119, 47, 26)","alignItems":"center","borderWidth":"0","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="" @tap="fuwuzhaopianTap">
				<view :style='{"padding":"0 10rpx 0 0","whiteSpace":"nowrap","color":"#000000","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">服务照片</view>
				<image :style='{"width":"80rpx","borderRadius":"100%","objectFit":"cover","display":"block","height":"80rpx"}' class="avator" v-if="ruleForm.fuwuzhaopian" :src="baseUrl+ruleForm.fuwuzhaopian.split(',')[0]" mode="aspectFill"></image>
				<image :style='{"width":"80rpx","borderRadius":"100%","objectFit":"cover","display":"block","height":"80rpx"}' class="avator" v-else src="../../static/gen/upload.png" mode="aspectFill"></image>
			</view>
			<view :style='{"padding":"12rpx 0","margin":"0 0 24rpx 0","borderColor":"rgb(119, 47, 26)","alignItems":"center","borderWidth":"0","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class=" select">
				<view :style='{"padding":"0 10rpx 0 0","whiteSpace":"nowrap","color":"#000000","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">服务时间</view>
				<picker  :disabled="ro.fuwushijian" :style='{"width":"100%","flex":"1","height":"auto"}' mode="date" :value="ruleForm.fuwushijian" @change="fuwushijianChange">
					<view :style='{"borderColor":"#707070","color":"#707070","borderWidth":"0 0 2rpx 0","width":"100%","lineHeight":"80rpx","fontSize":"28rpx","borderStyle":"solid"}' class="uni-input">{{ruleForm.fuwushijian?ruleForm.fuwushijian:"请选择服务时间"}}</view>
				</picker>
			</view>
			<view :style='{"padding":"12rpx 0","margin":"0 0 24rpx 0","borderColor":"rgb(119, 47, 26)","alignItems":"center","borderWidth":"0","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"padding":"0 10rpx 0 0","whiteSpace":"nowrap","color":"#000000","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">求助账号</view>
				<input :style='{"padding":"0px 24rpx","margin":"0px","borderColor":"#808080","color":"rgb(0, 0, 0)","flex":"1","borderWidth":"0 0 2rpx 0","lineHeight":"80rpx","fontSize":"28rpx","borderStyle":"solid"}' :disabled="ro.zhanghao" v-model="ruleForm.zhanghao" placeholder="老人账号"  type="text"></input>
			</view>
			<view :style='{"padding":"12rpx 0","margin":"0 0 24rpx 0","borderColor":"rgb(119, 47, 26)","alignItems":"center","borderWidth":"0","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"padding":"0 10rpx 0 0","whiteSpace":"nowrap","color":"#000000","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">求助者</view>
				<input :style='{"padding":"0px 24rpx","margin":"0px","borderColor":"#808080","color":"rgb(0, 0, 0)","flex":"1","borderWidth":"0 0 2rpx 0","lineHeight":"80rpx","fontSize":"28rpx","borderStyle":"solid"}' :disabled="ro.xingming" v-model="ruleForm.xingming" placeholder="老人姓名"  type="text"></input>
			</view>
			<view :style='{"padding":"12rpx 0","margin":"0 0 24rpx 0","borderColor":"rgb(119, 47, 26)","alignItems":"center","borderWidth":"0","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class=" select">
				<view :style='{"padding":"0 10rpx 0 0","whiteSpace":"nowrap","color":"#000000","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">服务状态</view>
				<picker :disabled="ro.fuwuzhuangtai" :style='{"width":"100%","flex":"1","height":"auto"}' @change="fuwuzhuangtaiChange" :value="fuwuzhuangtaiIndex" :range="fuwuzhuangtaiOptions">
					<view :style='{"borderColor":"#707070","color":"#707070","borderWidth":"0 0 2rpx 0","width":"100%","lineHeight":"80rpx","fontSize":"28rpx","borderStyle":"solid"}' class="uni-input">{{ruleForm.fuwuzhuangtai?ruleForm.fuwuzhuangtai:"请选择服务状态"}}</view>
				</picker>
			</view>
			<view :style='{"padding":"12rpx 0","margin":"0 0 24rpx 0","borderColor":"rgb(119, 47, 26)","alignItems":"center","borderWidth":"0","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"padding":"0 10rpx 0 0","whiteSpace":"nowrap","color":"#000000","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">服务账号</view>
				<input :style='{"padding":"0px 24rpx","margin":"0px","borderColor":"#808080","color":"rgb(0, 0, 0)","flex":"1","borderWidth":"0 0 2rpx 0","lineHeight":"80rpx","fontSize":"28rpx","borderStyle":"solid"}' :disabled="ro.yuangongzhanghao" v-model="ruleForm.yuangongzhanghao" placeholder="员工账号"  type="text"></input>
			</view>
			<view :style='{"padding":"12rpx 0","margin":"0 0 24rpx 0","borderColor":"rgb(119, 47, 26)","alignItems":"center","borderWidth":"0","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"padding":"0 10rpx 0 0","whiteSpace":"nowrap","color":"#000000","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">服务者</view>
				<input :style='{"padding":"0px 24rpx","margin":"0px","borderColor":"#808080","color":"rgb(0, 0, 0)","flex":"1","borderWidth":"0 0 2rpx 0","lineHeight":"80rpx","fontSize":"28rpx","borderStyle":"solid"}' :disabled="ro.yuangongxingming" v-model="ruleForm.yuangongxingming" placeholder="员工姓名"  type="text"></input>
			</view>
			<view :style='{"padding":"12rpx 0","margin":"0 0 24rpx 0","borderColor":"rgb(119, 47, 26)","alignItems":"center","borderWidth":"0","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"padding":"0 10rpx 0 0","whiteSpace":"nowrap","color":"#000000","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">获得积分</view>
				<input :style='{"padding":"0px 24rpx","margin":"0px","borderColor":"#808080","color":"rgb(0, 0, 0)","flex":"1","borderWidth":"0 0 2rpx 0","lineHeight":"80rpx","fontSize":"28rpx","borderStyle":"solid"}' :disabled="ro.jifen" v-model.number="ruleForm.jifen" placeholder="获得积分" type="digit"></input>
			</view>
            <view :style='{"padding":"12rpx 0","margin":"0 0 24rpx 0","borderColor":"rgb(119, 47, 26)","alignItems":"center","borderWidth":"0","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
                <view :style='{"padding":"0 10rpx 0 0","whiteSpace":"nowrap","color":"#000000","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">服务地址</view>
                <input :style='{"padding":"0px 24rpx","margin":"0px","borderColor":"#808080","color":"rgb(0, 0, 0)","flex":"1","borderWidth":"0 0 2rpx 0","lineHeight":"80rpx","fontSize":"28rpx","borderStyle":"solid"}' :disabled="ro.zhuzhi" v-model="ruleForm.zhuzhi" placeholder="请输入服务地址"  type="text"></input>
            </view>
			<view :style='{"padding":"12rpx 0","margin":"0 0 24rpx 0","borderColor":"rgb(119, 47, 26)","alignItems":"flex-start","borderWidth":"0","display":"flex","width":"100%","borderStyle":"solid","height":"auto"}' class="">
				<view :style='{"padding":"0 10rpx 0 0","whiteSpace":"nowrap","color":"#000000","textAlign":"right","width":"auto","lineHeight":"80rpx","fontSize":"28rpx","fontWeight":"600"}' class="title">服务内容</view>
				<textarea :style='{"padding":"20rpx 24rpx","margin":"0px","borderColor":"#808080","color":"rgb(0, 0, 0)","flex":"1","borderWidth":"2rpx","fontSize":"28rpx","borderRadius":"12rpx","borderStyle":"solid","height":"240rpx"}' v-model="ruleForm.fuwuneirong" placeholder="请输入服务内容" :maxlength="-1" readonly></textarea>
			</view>
			
			<view :style='{"width":"100%","justifyContent":"space-between","display":"flex","height":"auto"}' class="btn" >
				<button :style='{"border":"0","padding":"0px","margin":"0","color":"#fff","background":"linear-gradient( 135deg, #9EF0EA 0%, #99ABFF 100%)","width":"48%","lineHeight":"80rpx","fontSize":"28rpx","height":"80rpx"}' @tap="onSubmitTap" class="bg-red">提交</button>
			</view>
		</form>
	</view>
</view>
</template>

<script>
	import wPicker from "@/components/w-picker/w-picker.vue";
	import xiaEditor from '@/components/xia-editor/xia-editor';
	import multipleSelect from "@/components/momo-multipleSelect/momo-multipleSelect";
	export default {
		data() {
			return {
				cross:'',
				ruleForm: {
				fuwubiaoti: '',
				fuwuleixing: '',
				fuwuzhaopian: '',
				fuwushijian: '',
				fuwuneirong: '',
				zhanghao: '',
				zhuzhi:'',
				xingming: '',
				fuwuzhuangtai: '待处理',
				yuangongzhanghao: '',
				yuangongxingming: '',
					jifen: Number('20') ,
				shhf: '',
				},
				fuwuleixingOptions: [],
				fuwuleixingIndex: 0,
				fuwuzhuangtaiOptions: [],
				fuwuzhuangtaiIndex: 0,
				// 登录用户信息
				user: {},
				ro:{
				   fuwubiaoti : false,
				   fuwuleixing : false,
				   fuwuzhaopian : false,
				   fuwushijian : false,
				   fuwuneirong : true,
				   zhanghao : false,
				   xingming : false,
				   fuwuzhuangtai : false,
				   yuangongzhanghao : false,
				   yuangongxingming : false,
				   jifen : false,
				   shhf : false,
				   zhuzhi:false,
				},
				virtualPay: false,
			}
		},
		components: {
			wPicker,
			xiaEditor,
			multipleSelect,
		},
		computed: {
			baseUrl() {
				return this.$base.url;
			},



		},
		async onLoad(options) {
			 this.fuwuzhuangtaiOptions = "服务中".split(',')
			if(options.virtualPay){
				this.virtualPay = true
			}
			this.ruleForm.fuwushijian = this.$utils.getCurDate();
			let table = uni.getStorageSync("nowTable");
			// 获取用户信息
			let res = await this.$api.session(table);
			this.user = res.data;
			
			// ss读取
			this.ruleForm.zhanghao = this.user.zhanghao
			this.ro.zhanghao = true;
			this.ruleForm.xingming = this.user.xingming
			this.ro.xingming = true;
			this.ruleForm.yuangongzhanghao = this.user.yuangongzhanghao
			this.ro.yuangongzhanghao = true;
			this.ruleForm.yuangongxingming = this.user.yuangongxingming
			this.ro.yuangongxingming = true;
			this.ro.jifen = true;
			this.ruleForm.zhuzhi = this.user.zhuzhi;
			  this.ro.zhuzhi = true;
			// 下拉框
			res = await this.$api.option(`fuwuleixing`,`fuwuleixing`,{});
			this.fuwuleixingOptions = res.data;
			this.fuwuleixingOptions.unshift("请选择服务类型");
			// 如果有登录，获取登录后保存的userid
			this.ruleForm.userid = uni.getStorageSync("appUserid")
			if (options.refid) {
				// 如果上一级页面传递了refid，获取改refid数据信息
				this.ruleForm.refid = Number(options.refid);
				this.ruleForm.nickname = uni.getStorageSync("nickname");
			}
			// 如果是更新操作
			if (options.id) {
				this.ruleForm.id = options.id;
				// 获取信息
				res = await this.$api.info(`xuqiuchuli`, this.ruleForm.id);
				this.ruleForm = res.data;
			}
			// 跨表
			this.cross = options.cross;
			if(options.cross){
				var obj = uni.getStorageSync('crossObj');
			
				
				for (var o in obj){
					if(o=='fuwubiaoti'){
						this.ruleForm.fuwubiaoti = obj[o];
						this.ro.fuwubiaoti = true;
						continue;
					}
					if(o=='fuwuleixing'){
						this.ruleForm.fuwuleixing = obj[o];
						this.ro.fuwuleixing = true;
						continue;
					}
					if(o=='fuwuzhaopian'){
						this.ruleForm.fuwuzhaopian = obj[o].split(",")[0];
						this.ro.fuwuzhaopian = true;
						continue;
					}
					if(o=='fuwushijian'){
						this.ruleForm.fuwushijian = obj[o];
						this.ro.fuwushijian = true;
						continue;
					}
					if(o=='fuwuneirong'){
						this.ruleForm.fuwuneirong = obj[o];
						this.ro.fuwuneirong = true;
						continue;
					}
					if(o=='zhanghao'){
						this.ruleForm.zhanghao = obj[o];
						this.ro.zhanghao = true;
						continue;
					}
					if(o=='xingming'){
						this.ruleForm.xingming = obj[o];
						this.ro.xingming = true;
						continue;
					}
					if(o=='fuwuzhuangtai'){
						this.ruleForm.fuwuzhuangtai = obj[o];
						// this.ro.fuwuzhuangtai = true;
						continue;
					}
					if(o=='yuangongzhanghao'){
						this.ruleForm.yuangongzhanghao = obj[o];
						this.ro.yuangongzhanghao = true;
						continue;
					}
					if(o=='yuangongxingming'){
						this.ruleForm.yuangongxingming = obj[o];
						this.ro.yuangongxingming = true;
						continue;
					}
					if(o=='jifen'){
						this.ruleForm.jifen = obj[o];
						this.ro.jifen = true;
						continue;
					}
				}
			}
			this.styleChange()
			this.$forceUpdate()
			if (uni.getStorageSync('raffleType') && uni.getStorageSync('raffleType') != null) {
				uni.removeStorageSync('raffleType')
				setTimeout(() => {
					this.onSubmitTap()
				}, 300)
			}
		},
		methods: {
			styleChange() {
				this.$nextTick(()=>{
					// document.querySelectorAll('.app-update-pv . .uni-input-input').forEach(el=>{
					//   el.style.backgroundColor = this.addUpdateForm.input.content.backgroundColor
					// })
				})
			},

			// 多级联动参数

			fuwushijianChange(e) {
				this.ruleForm.fuwushijian = e.target.value;
				this.$forceUpdate();
			},


			// 下拉变化
			fuwuleixingChange(e) {
				this.fuwuleixingIndex = e.target.value
				this.ruleForm.fuwuleixing = this.fuwuleixingOptions[this.fuwuleixingIndex]
			},
			// 下拉变化
			fuwuzhuangtaiChange(e) {
				this.fuwuzhuangtaiIndex = e.target.value
				this.ruleForm.fuwuzhuangtai = this.fuwuzhuangtaiOptions[this.fuwuzhuangtaiIndex]
			},

			fuwuzhaopianTap() {
				let _this = this;
				this.$api.upload(function(res) {
					_this.ruleForm.fuwuzhaopian = 'upload/' + res.file;
					_this.$forceUpdate();
					_this.$nextTick(()=>{
						_this.styleChange()
					})
				});
			},

			getUUID () {
				return new Date().getTime();
			},
			async onSubmitTap() {
				const crossObj = uni.getStorageSync("crossObj");
				if (!crossObj) {
				    this.$utils.msg("未找到跨表信息");
				    return;
				}
				if (crossObj.id) {
				    this.ruleForm.fuwuId = crossObj.id;
				} else if (crossObj.data && crossObj.data.id) { // 兼容旧结构（如有）
				    this.ruleForm.fuwuId = crossObj.data.id;
				} else {
				    this.$utils.msg("跨表信息不完整");
				    return;
				}
				let res = await this.$api.update(`xuqiuchuli`, this.ruleForm);			
				let that = this
				//跨表计算判断
				var obj;
				if((!this.ruleForm.fuwuzhuangtai)){
					this.$utils.msg(`服务状态不能为空`);
					return
				}
				if(this.ruleForm.jifen&&(!this.$validate.isNumber(this.ruleForm.jifen))){
					this.$utils.msg(`获得积分应输入数字`);
					return
				}
				//更新跨表属性
				var crossuserid;
				var crossrefid;
				var crossoptnum;
				if(this.cross){
					uni.setStorageSync('crossCleanType',true);
					var statusColumnName = uni.getStorageSync('statusColumnName');
					var statusColumnValue = uni.getStorageSync('statusColumnValue');
					if(statusColumnName!='') {
						if(!obj) {
							obj = uni.getStorageSync('crossObj');
						}
						if(!statusColumnName.startsWith("[")) {
							for (var o in obj){
								if(o==statusColumnName){
									obj[o] = statusColumnValue;
								}

							}
							var table = uni.getStorageSync('crossTable');
							await this.$api.update(`${table}`, obj);
						} else {
							   crossuserid=Number(uni.getStorageSync('appUserid'));
							   crossrefid=obj['id'];
							   crossoptnum=uni.getStorageSync('statusColumnName');
							   crossoptnum=crossoptnum.replace(/\[/,"").replace(/\]/,"");
						}
					}
				}
				if(crossrefid && crossuserid) {
					this.ruleForm.crossuserid=crossuserid;
					this.ruleForm.crossrefid=crossrefid;
					let params = {
						page: 1,
						limit:10,
						crossuserid:crossuserid,
						crossrefid:crossrefid,
					}
					let res = await this.$api.list(`xuqiuchuli`, params);
					if (res.data.total >= crossoptnum) {
						this.$utils.msg(uni.getStorageSync('tips'));
						uni.removeStorageSync('crossCleanType');
						return false;
					} else {
				//跨表计算
						let oet = {}
						if(this.ruleForm.id){
							await this.$api.update(`xuqiuchuli`, this.ruleForm);
						}else{
							oet = await this.$api.add(`xuqiuchuli`, this.ruleForm);
						 if(this.user.jifen>=0) {
							 this.user.jifen = parseFloat(this.user.jifen)
							 await this.$api.update(uni.getStorageSync("nowTable"), this.user);
						 }
						}

						this.$utils.msgBack('提交成功');
					}
				} else {
				//跨表计算
					let oet = {}
					if(this.ruleForm.id){
						await this.$api.update(`xuqiuchuli`, this.ruleForm);
					}else{
						oet = await this.$api.add(`xuqiuchuli`, this.ruleForm);
						 if(this.user.jifen>=0) {
							 this.user.jifen = parseFloat(this.user.jifen) 
							 await this.$api.update(uni.getStorageSync("nowTable"), this.user);
						 }
					}

					this.$utils.msgBack('提交成功');
				}
			},
			optionsChange(e) {
				this.index = e.target.value
			},
			bindDateChange(e) {
				this.date = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			toggleTab(str) {
				if(this.ro[str]){
					return false
				}
				this.$refs[str].show();
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		min-height: calc(100vh - 44px);
		box-sizing: border-box;
	}
</style>
